﻿<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:wpfExtended="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
             xmlns:p="clr-namespace:BaMusaSoft.RPS.View.Properties" x:Name="MyParent" x:Class="BaMusaSoft.RPS.View.ScheduleView"
             mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="750" Loaded="ViewLoaded" Unloaded="ViewUnloaded"
             
             FlowDirection="RightToLeft" FontSize="14">

  <UserControl.Resources>
    <ContextMenu x:Key="gridRowContextMenu">
      <MenuItem Header="حذف"
                DataContext="{Binding  RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=DataContext}"
                Command="{Binding DeleteDetailCommand}" />
    </ContextMenu>
    <DataTemplate x:Key="DetailsStatistics">
      <Grid Background="Beige">
        <Grid.RowDefinitions>
          <RowDefinition />
          <RowDefinition />
          <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Grid.Row="0" Text="{x:Static p:Resources.DetailStatiDueSum}" TextAlignment="Right"
                   Margin="3,3,0,3" />
        <TextBlock Grid.Column="0" Grid.Row="1" Text="{x:Static p:Resources.DetailStatiPaidSum}" TextAlignment="Right"
                   Margin="3,3,0,3" />
        <TextBlock Grid.Column="0" Grid.Row="2" Text="{x:Static p:Resources.DetailStatiPaidPercent}"
                   TextAlignment="Right" Margin="3,3,0,3" />
        <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=AmountDueSum, Mode=OneWay}" IsEnabled="False"
                 Margin="10,3,0,3" Width="150" HorizontalAlignment="Left" Height="30" />
        <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=AmountPaidSum, Mode=OneWay}" IsEnabled="False"
                 Margin="10,0,0,3" Width="150" HorizontalAlignment="Left" Height="30" />
        <TextBox Grid.Column="1" Grid.Row="2"
                 Text="{Binding Path=AmountPaidPercent, Mode=OneWay, StringFormat={}{0:0.00}%}" Margin="10,0,0,3" IsEnabled="False"
                 Width="150" HorizontalAlignment="Left" Height="30" />
      </Grid>
    </DataTemplate>
    <DataTemplate x:Key="ScheduleRemarks">
      <StackPanel Orientation="Vertical">
        <TextBlock Text="{x:Static p:Resources.RemarksLabel}" Margin="3" />
        <TextBox Text="{Binding Path=Remarks, UpdateSourceTrigger=PropertyChanged}" MinHeight="65" Margin="5,0,5,5"
                 AcceptsReturn="True" />
      </StackPanel>
    </DataTemplate>
        
    </UserControl.Resources>
  <UserControl.InputBindings>
    <!--Shortcuts for commands-->
    <KeyBinding Key="N" Modifiers="Control" Command="{Binding CreateNewCommand}" />
    <KeyBinding Key="S" Modifiers="Control" Command="{Binding SaveCommand}" />
    <KeyBinding Key="P" Modifiers="Control" Command="{Binding PrintCommand}" />
    <KeyBinding Key="F2" Modifiers="Control" Command="{Binding ElementName=MyParent, Path=AddContractCommand}" />
    <KeyBinding Key="F3" Modifiers="Control" Command="{Binding ElementName=MyParent, Path=AddSignerCommand}" />
    <KeyBinding Key="F4" Modifiers="Control" Command="{Binding ElementName=MyParent, Path=AddDetailCommand}" />

  </UserControl.InputBindings>

  <Grid x:Name="LayoutRoot">
    <Grid x:Name="ContentGrid" PreviewKeyDown="OnContentGridKeyDown">
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition />
        <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.50*" />
        <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>

            <ToolBar Grid.ColumnSpan="2" Grid.Row="0" KeyboardNavigation.TabNavigation="Continue" Margin="0,5,0,0">
                <Button Command="{Binding CreateNewCommand}" Margin="3" Style="{StaticResource AddNewButtonStyle}"/>
                <Button Command="{Binding SaveCommand}" Margin="3" Style="{StaticResource SaveButtonStyle}"/>
                <Button Command="{Binding PrintCommand}" Margin="3" Style="{StaticResource PrintButtonStyle}"/>
                <Button Command="{Binding DeleteCommand}" Margin="3" Style="{StaticResource DeleteButtonStyle}"/>
                <Button Command="{Binding RefreshCommand}" Margin="3" Style="{StaticResource RefreshButtonStyle}"/>
            </ToolBar>
            <Grid Grid.Row="1" Grid.Column="1" Margin="15,5,15,10">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Text="بحث" Margin="5,0" />
        <ComboBox Grid.Column="1" x:Name="cmbSearch" IsEditable="True"
                  IsDropDownOpen="{Binding SearchSchedule.ShowResult}" ItemsSource="{Binding SearchSchedule.Result}"
                  Text="{Binding Path= SearchCriteria, UpdateSourceTrigger=PropertyChanged}"
                  SelectionChanged="OnSearchBoxSelectionChanged" DisplayMemberPath="Contract.Customer.Name" Height="30"></ComboBox>
        <Button Grid.Column="2" Command="{Binding SearchCommand}" Content="{StaticResource FindImage}" IsDefault="True" />
      </Grid>

      <Grid Grid.Column="0" Grid.Row="2">
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <GroupBox Grid.Column="0" Grid.Row="0" Margin="10,0,10,10" BorderBrush="Silver" BorderThickness="1"
                  Header="{x:Static p:Resources.ScheduleInfoTitle}">
          <Grid>
            <Grid.RowDefinitions>
              <RowDefinition />
              <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width=".500*" />
              <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=ScheduleId}" Margin="4,10,5,4"
                       TextAlignment="Left" FontSize="20" FontWeight="Bold" Foreground="Red" Height="30" />
            <TextBlock Grid.Column="0" Grid.Row="1" Text="{x:Static p:Resources.ScheduleRegiLabel}" Margin="4,10,5,4"
                       TextAlignment="Left" />
            <wpfExtended:MaskedTextBox Grid.Column="1" Grid.Row="1" x:Name="txtRegDate" Margin="4,10,5,4"
                                       IncludeLiterals="False" IncludePrompt="False"
                                       Value="{Binding Path=RegistredAt, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DateConverter}, ValidatesOnDataErrors=True}"
                                       Mask="##/ ## /####" Height="30" />
          </Grid>
        </GroupBox>
        <GroupBox Grid.Column="0" Grid.Row="1" Margin="10" BorderBrush="Silver" BorderThickness="1"
                  Header="{x:Static p:Resources.PropertyInfoTitle}" KeyboardNavigation.TabNavigation="Local">
          <Grid>
            <Grid.RowDefinitions>
              <RowDefinition />
              <RowDefinition />
              <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width=".500*" />
              <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Button Grid.Column="1" Grid.Row="0" HorizontalAlignment="Right" ToolTip="إدراج عقد ( Ctrl + F2  )"
                    Content="{StaticResource AddImage}" Command="{Binding ElementName=MyParent, Path=AddContractCommand}" />
            <TextBlock Grid.Column="0" Grid.Row="1" Text="{x:Static p:Resources.ContractPropertyNoLabel}"
                       Margin="4,10,5,4" TextAlignment="Left" />
            <TextBlock Grid.Column="0" Grid.Row="2" Text="{x:Static p:Resources.CustomerNameLabel}" Margin="4,10,5,4"
                       TextAlignment="Left" />
            <TextBox x:Name="txtPropertyNo" Grid.Column="1" Grid.Row="1"
                     Text="{Binding PropertyNo, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                     Margin="4,10,5,4" KeyboardNavigation.TabIndex="2" Height="30"></TextBox>
            <TextBox Grid.Column="1" Grid.Row="2"
                     Text="{Binding CustomerName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                     IsEnabled="False" IsTabStop="False" Margin="4,10,5,4" Height="30" />
          </Grid>
        </GroupBox>

        <GroupBox Grid.Row="2" Header="{x:Static p:Resources.SignerInfoTitle}" Margin="10">
          <Grid KeyboardNavigation.TabNavigation="Local">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width=".500*" />
              <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition />
              <RowDefinition />
              <RowDefinition />
              <RowDefinition />
              <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Grid.Row="1" Text="{x:Static p:Resources.SignerIdNumberLable}" Margin="4,10,5,4"
                       TextAlignment="Left" />
            <TextBlock Grid.Column="0" Grid.Row="2" Text="{x:Static p:Resources.SignerNameLabel}" Margin="4,10,5,4"
                       TextAlignment="Left" />
            <TextBlock Grid.Column="0" Grid.Row="3" Text="{x:Static p:Resources.SignerMobileLabel}" Margin="4,10,5,4"
                       TextAlignment="Left" />
            <TextBlock Grid.Column="0" Grid.Row="4" Text="{x:Static p:Resources.SignerPhoneLabel}" Margin="4,10,5,4"
                       TextAlignment="Left" />
            <TextBox x:Name="txtSignerId" Grid.Column="1" Grid.Row="1"
                     Text="{Binding SignerId, Mode=OneWay, ValidatesOnDataErrors=True}" MaxLength="10" Margin="4,10,5,4"
                     KeyboardNavigation.TabIndex="3" IsEnabled="True" Height="30" />

            <TextBox Grid.Column="1" Grid.Row="2"
                     Text="{Binding Path=SignerName, Mode=OneWay, ValidatesOnDataErrors=True}" IsEnabled="False" IsTabStop="False"
                     Margin="4,10,5,4" Height="30" />
            <TextBox Grid.Column="1" Grid.Row="3"
                     Text="{Binding Path=SignerMobile, Mode=OneWay, ValidatesOnDataErrors=True}" IsEnabled="False" IsTabStop="False"
                     Margin="4,10,5,4" Height="30" />
            <TextBox Grid.Column="1" Grid.Row="4"
                     Text="{Binding Path=SignerPhone, Mode=OneWay, ValidatesOnDataErrors=True}" IsEnabled="False" IsTabStop="False"
                     Margin="4,10,5,4" Height="30" />
            <Button Grid.Column="1" Grid.Row="0" Content="{StaticResource AddImage}"
                    ToolTip="إدراج متعهد ( Ctrl + F3  )" Command="{Binding ElementName=MyParent, Path=AddSignerCommand}"
                    HorizontalAlignment="Right" />
          </Grid>
        </GroupBox>
      </Grid>
      <Border Grid.Column="1" Grid.Row="2" BorderBrush="Silver" BorderThickness="1" CornerRadius="2"
              Margin="15,10,15,0" Padding="5">
        <Grid KeyboardNavigation.TabNavigation="Local" Margin="0">
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
          </Grid.RowDefinitions>
          <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Right">
            <TextBlock Text="{x:Static p:Resources.ScheduleDetailTitle}" Margin="3,0,30,3" />
            <Button Content="{StaticResource AddImage}" Margin="3,0,0,3" ToolTip="إضافة دفعات تعهد ( Ctrl + F4  )"
                    Command="{Binding ElementName=MyParent, Path=AddDetailCommand}" />
          </StackPanel>
          <DataGrid x:Name="dgDetails" Grid.Row="1" KeyboardNavigation.TabIndex="4" AutoGenerateColumns="False"
                    SelectionUnit="CellOrRowHeader" SelectionMode="Single" RowHeaderWidth="25"
                    ItemsSource="{Binding Path=Details, ValidatesOnDataErrors=True}"
                    CanUserSortColumns="False" CanUserAddRows="False" SelectionChanged="OnDetailsGridSelectionChanged"
                    >
                    
            <DataGrid.Columns>
              <DataGridTextColumn Header="{x:Static p:Resources.AmountDueLabel}" MinWidth="50"
                                  Binding="{Binding AmountDue, UpdateSourceTrigger=PropertyChanged}" Width="*" />
              <DataGridTemplateColumn Header="{x:Static p:Resources.DateDueLabel}" Width="2*"
                                      CellEditingTemplate="{StaticResource EditMaskedCellTemplate}" 
                                      CellTemplate="{StaticResource ShowMaskCellTemplate}" />
              <DataGridTextColumn Header="{x:Static p:Resources.PaidLabel}" MinWidth="50"
                                  Binding="{Binding AmountPaid}" Width="*" />
              <DataGridTextColumn Header="{x:Static p:Resources.BalanceLabel}" MinWidth="50"
                                  Binding="{Binding Balance}" IsReadOnly="True" Width="*" />
              <DataGridCheckBoxColumn Header="{x:Static p:Resources.DiscountAmountLabel}"
                                      Binding="{Binding DiscountAmount}" />
              <DataGridTextColumn Header="{x:Static p:Resources.RemarksLabel}" MinWidth="75"
                                  Binding="{Binding Remarks}" Width="*" />

            </DataGrid.Columns>
            <!--<DataGrid.RowStyle>
                            <Style TargetType="DataGridRow">
                                <Style.Triggers>
                                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                                        <Setter Property="HeaderStyle" Value="{StaticResource FocusedRowHeader}"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </DataGrid.RowStyle>-->

            <!--Attach the context menu to the row header only, so the use will be able to delete the row only when it's fully selected.-->
            <DataGrid.RowHeaderStyle>
              <Style TargetType="DataGridRowHeader" BasedOn="{StaticResource {x:Type DataGridRowHeader}}">
                <Setter Property="ContextMenu" Value="{StaticResource gridRowContextMenu}" />
              </Style>
            </DataGrid.RowHeaderStyle>
          </DataGrid>

          <ContentControl Grid.Row="2" Content="{Binding Path=Statistics}"
                          ContentTemplate="{StaticResource DetailsStatistics}" IsTabStop="False" />
          <ContentControl Grid.Row="3" Content="{Binding}" ContentTemplate="{StaticResource ScheduleRemarks}"
                          IsTabStop="False" />
        </Grid>
      </Border>

      <HeaderedContentControl Header="{x:Static p:Resources.ContractInfoTitle}" Grid.ColumnSpan="2" Grid.Row="3"
                              Margin="10,15,15,15" IsTabStop="False">
        <DataGrid AutoGenerateColumns="False" ScrollViewer.CanContentScroll="False" IsReadOnly="True"
                  Padding="0,0,10,0" RowHeaderWidth="25" ItemsSource="{Binding ContractWrapper}" IsTabStop="False" Margin="5">
          <DataGrid.Columns>
            <DataGridTextColumn Header="{x:Static p:Resources.ContractNoLabel}" MinWidth="50" Width="*"
                                Binding="{Binding ContractNo}" />
            <DataGridTextColumn Header="{x:Static p:Resources.ContractPropertyDescLabel}" MinWidth="100" Width="2*"
                                Binding="{Binding Path= Property.Description}" />
            <DataGridTextColumn Header="{x:Static p:Resources.ContractRentLabel}" Binding="{Binding RentBalance}"
                                MinWidth="50" Width="*" />
            <DataGridTextColumn Header="{x:Static p:Resources.ContractMaintLabel}"
                                Binding="{Binding MaintenanceBalance}" MinWidth="30" Width="*" />
            <DataGridTextColumn Header="{x:Static p:Resources.ContractDepositLabel}" Binding="{Binding DepositBalance}"
                                MinWidth="30" Width="*" />
            <DataGridTextColumn Header="{x:Static p:Resources.ContractTotalLabel}" Binding="{Binding Total}"
                                MinWidth="50" Width="*" />
          </DataGrid.Columns>
        </DataGrid>

      </HeaderedContentControl>
    </Grid>
  </Grid>

</UserControl>